package com.sk.bilibili.tool;

import com.sk.bilibili.bean.Banner;
import com.sk.bilibili.bean.Them;
import com.sk.bilibili.bean.ThemBanner;
import com.sk.bilibili.bean.VideoSet;
import com.sk.bilibili.service.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author 邵康
 * @date 2021/12/16 16:07
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class UpdateBanner {

    @Autowired
    private MinioService minioService;
    @Autowired
    private ThemService themService;
    private static final String SMALLCOVER_PATH="them/smallCover/%s.jpg";
    private static final String VERTICALCOVER_PATH="them/verticalCover/%s.jpg";
    @Autowired
    private VideoSetService videoSetService;
    private static final String COVER_PATH="videoSet/cover/%s.jpg";
    private static final Pattern COVER_PATTERN=Pattern.compile("/(videoSet/cover/.*jpg)");
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private ThemBannerService themBannerService;
    @Autowired
    private BannerService bannerService;

    @Test//按播放量 更新  番剧轮播图
    void test4(){
        List<ThemBanner> list = themBannerService.list(null);
        List<Them> query = jdbcTemplate.query("SELECT them.* FROM them LEFT JOIN video_set ON " +
                "them.video_set_id = video_set.id ORDER BY play_num LIMIT "+list.size(), new BeanPropertyRowMapper<>(Them.class));
        for (int i = 0; i < list.size(); i++) {
            ThemBanner themBanner = list.get(i);
            themBanner.setUrl("play/"+query.get(i).getId());
            themBannerService.updateById(themBanner);
        }
    }

    @Test// 按播放量更新普通轮播图
    void test3(){
        List<Banner> list = bannerService.list(null);
        List<VideoSet> query = jdbcTemplate.query("SELECT * FROM video_set WHERE type_id not IN (2,3) " +
                "order BY play_num DESC LIMIT "+list.size(), new BeanPropertyRowMapper<>(VideoSet.class));
        for (int i = 0; i < list.size(); i++) {
            Banner banner = list.get(i);
            banner.setUrl("video/"+query.get(i).getId());
            bannerService.updateById(banner);
        }
    }

    @Test
    void test2(){
        videoSetService.list(null).stream().map(VideoSet::getCover).distinct()
                .forEach(s -> minioService.delete(s,COVER_PATTERN));
        List<String> files=new ArrayList<>();
        File dirs = new File("C:\\Users\\邵康\\Desktop\\新建文件夹");
        File[] files1 = dirs.listFiles();
        for (int i = 0; i < files1.length; i++) {
            files.add(minioService.uploadStream(files1[i],COVER_PATH));
        }
        List<VideoSet> list = videoSetService.list(null);
        int index=0;
        for (VideoSet videoSet:list){
            videoSet.setCover(files.get(index%files.size()));
            index++;
            videoSetService.updateById(videoSet);
        }
    }

    @Test
    void test1(){
        List<String> list = themService.list(null).stream()
                .map(Them::getVerticalCover).distinct().collect(Collectors.toList());
        File file = new File("D:\\谷歌浏览器下载\\图片助手(ImageAssistant)_批量图片下载器\\www.bilibili.com\\2021-12-21_21-22-14\\哔哩哔哩排行榜");
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            list.add(minioService.uploadStream(files[i],VERTICALCOVER_PATH));
        }
        List<Them> thems = themService.list(null);
        int index=0;
        for (Them them:thems){
            them.setVerticalCover(list.get(index%list.size()));
            index++;
            themService.updateById(them);
        }
    }


}
